{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "440d361e-f41e-494a-98a3-aa87cf0087af",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-19T19:58:52.702052Z",
     "end_time": "2023-12-19T19:58:53.242008Z"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "       国家 2018年人口数量\n32  China     1.42B\n79  India     1.37B",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家</th>\n      <th>2018年人口数量</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>32</th>\n      <td>China</td>\n      <td>1.42B</td>\n    </tr>\n    <tr>\n      <th>79</th>\n      <td>India</td>\n      <td>1.37B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 文件来自https://www.gapminder.org/data/\n",
    "population_total = pd.read_excel('population_total.xlsx')\n",
    "df1 = population_total[['country', 2018]].query('country in (\"China\",\"India\")')\n",
    "df1.columns = ['国家', '2018年人口数量']\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T19:58:53.244006Z",
     "end_time": "2023-12-19T19:58:54.027859Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "         国家  2018年预期寿命\n32    China       77.4\n87    Japan       84.8\n147  Russia       73.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家</th>\n      <th>2018年预期寿命</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>32</th>\n      <td>China</td>\n      <td>77.4</td>\n    </tr>\n    <tr>\n      <th>87</th>\n      <td>Japan</td>\n      <td>84.8</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>Russia</td>\n      <td>73.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 文件来自https://www.gapminder.org/data/\n",
    "life_expectancy_years = pd.read_excel('life_expectancy_years.xlsx')\n",
    "df2 = life_expectancy_years[['country', 2018]].query('country in (\"China\",\"Japan\",\"Russia\")')\n",
    "df2.columns = ['国家', '2018年预期寿命']\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T19:59:07.180379Z",
     "end_time": "2023-12-19T19:59:07.756764Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "       国家 2018年人口数量  2018年预期寿命\n0   China     1.42B       77.4\n1   India     1.37B        NaN\n2   Japan       NaN       84.8\n3  Russia       NaN       73.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家</th>\n      <th>2018年人口数量</th>\n      <th>2018年预期寿命</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>77.4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>India</td>\n      <td>1.37B</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Japan</td>\n      <td>NaN</td>\n      <td>84.8</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Russia</td>\n      <td>NaN</td>\n      <td>73.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 外连接\n",
    "df1.merge(df2, how='outer', on='国家')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T20:17:25.485252Z",
     "end_time": "2023-12-19T20:17:25.512139Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "      国家 2018年人口数量  2018年预期寿命\n0  China     1.42B       77.4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家</th>\n      <th>2018年人口数量</th>\n      <th>2018年预期寿命</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>77.4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 内连接\n",
    "df1.merge(df2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T20:18:43.259801Z",
     "end_time": "2023-12-19T20:18:43.304847Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "      国家 2018年人口数量  2018年预期寿命\n0  China     1.42B       77.4\n1  India     1.37B        NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家</th>\n      <th>2018年人口数量</th>\n      <th>2018年预期寿命</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>77.4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>India</td>\n      <td>1.37B</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 左连接\n",
    "df1.merge(df2, how='left')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T20:19:40.519237Z",
     "end_time": "2023-12-19T20:19:40.555692Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "       国家 2018年人口数量  2018年预期寿命\n0   China     1.42B       77.4\n1   Japan       NaN       84.8\n2  Russia       NaN       73.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家</th>\n      <th>2018年人口数量</th>\n      <th>2018年预期寿命</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>77.4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Japan</td>\n      <td>NaN</td>\n      <td>84.8</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Russia</td>\n      <td>NaN</td>\n      <td>73.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 右连接\n",
    "df1.merge(df2, how='right')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T20:20:24.957904Z",
     "end_time": "2023-12-19T20:20:25.000292Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "    国家_x 2018年人口数量    国家_y  2018年预期寿命\n0  China     1.42B   China       77.4\n1  China     1.42B   Japan       84.8\n2  China     1.42B  Russia       73.0\n3  India     1.37B   China       77.4\n4  India     1.37B   Japan       84.8\n5  India     1.37B  Russia       73.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家_x</th>\n      <th>2018年人口数量</th>\n      <th>国家_y</th>\n      <th>2018年预期寿命</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>China</td>\n      <td>77.4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>Japan</td>\n      <td>84.8</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>Russia</td>\n      <td>73.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>India</td>\n      <td>1.37B</td>\n      <td>China</td>\n      <td>77.4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>India</td>\n      <td>1.37B</td>\n      <td>Japan</td>\n      <td>84.8</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>India</td>\n      <td>1.37B</td>\n      <td>Russia</td>\n      <td>73.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交叉连接\n",
    "df1.assign(key=1).merge(df2.assign(key=1), on='key').drop('key', axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T20:25:07.834083Z",
     "end_time": "2023-12-19T20:25:07.846908Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "         国家 2018年人口数量  2018年预期寿命\n32    China     1.42B        NaN\n79    India     1.37B        NaN\n32    China       NaN       77.4\n87    Japan       NaN       84.8\n147  Russia       NaN       73.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>国家</th>\n      <th>2018年人口数量</th>\n      <th>2018年预期寿命</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>32</th>\n      <td>China</td>\n      <td>1.42B</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>79</th>\n      <td>India</td>\n      <td>1.37B</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>32</th>\n      <td>China</td>\n      <td>NaN</td>\n      <td>77.4</td>\n    </tr>\n    <tr>\n      <th>87</th>\n      <td>Japan</td>\n      <td>NaN</td>\n      <td>84.8</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>Russia</td>\n      <td>NaN</td>\n      <td>73.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 联合拼接\n",
    "pd.concat([df1, df2], sort=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-19T20:27:00.979939Z",
     "end_time": "2023-12-19T20:27:01.020029Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
